var i = 1;
var totalPage = "";
$(function(){
	var token=localStorage.getItem("token");
	$("#createTimeFrom").datetimepicker({lang:'ch',
		timepicker:false,
		format:'Y-m-d',
		formatDate:'Y-m-d'
	});
	$("#createTimeTo").datetimepicker({lang:'ch',
		timepicker:false,
		format:'Y-m-d',
		formatDate:'Y-m-d'
	});
	initTablit();

	gridObj = $.fn.bsgrid.init('g_tabYeh', {
		ajaxType : 'post',
		url :  server_url+'/rest/operMessage/operMessageList.do?ran=' + Math.random() + '&token=' + token,
		autoLoad : true,
		pageSizeSelect : true,
		stripeRows : true,
		pageSize : 10,
		displayBlankRows : false,
		isProcessLockScreen : true,
		rowSelectedColor : false,
		showPageToolbar:true,
		rowHoverColor:true
	});


});
function operate(record, rowIndex, colIndex, options) {
	//console.log(record);
	if(record.type=='1'){
		if(record.isRead=='1') {
			return '<div style="display: inline-block;">买家<span style="color: red;">' + record.usersName + '</span>发布了新的需求单,<a href="javascript:transferDemand(\'' + gridObj.getRecordIndexValue(record, 'orderId') + '\',\''+  gridObj.getRecordIndexValue(record, 'id')+'\');">前去审核</a>' + '<span style="color: red;">未读</span></div>';
		}
		else{
			return '<div style="display: inline-block;">买家<span style="color: red;">' + record.usersName + '</span>发布了新的需求单,<a href="javascript:transferDemand(\'' + gridObj.getRecordIndexValue(record, 'orderId') + '\');">前去审核</a></div>';
		}
	}
	else if(record.type=='2'){
		if(record.isRead=='1') {
			return '<div style="display: inline-block;">用户<span style="color: red;">' + record.usersName + '</span>直接购买了商品,请点击<a href="javascript:transferOrderModify(\'' + gridObj.getRecordIndexValue(record, 'orderId') + '\',\''+  gridObj.getRecordIndexValue(record, 'id')+'\');">查看详情</a>' + '<span style="color: red;">未读</span></div>';
		}
		else {
			return '<div style="display: inline-block;">用户<span style="color: red;">' + record.usersName + '</span>直接购买了商品,请点击<a href="javascript:transferOrderModify(\'' + gridObj.getRecordIndexValue(record, 'orderId') + '\');">查看详情</a></div>';
		}
	}
	else if(record.type=='3'){
		if(record.source=='2')
		{
			if(record.isRead=='1'){
				return '<div style="display: inline-block;">用户<span style="color: red;">'+record.supplyName+'</span>(卖家)发布了商品，请点击<a href="javascript:transferSkuProductModify(\'' + gridObj.getRecordIndexValue(record, 'skuProductId') + '\',\''+  gridObj.getRecordIndexValue(record, 'id')+'\');">进行审核</a>'+'<span style="color: red;">未读</span></div>';
			}
			else {
				return '<div style="display: inline-block;">用户<span style="color: red;">'+record.supplyName+'</span>(卖家)发布了商品，请点击<a href="javascript:transferSkuProductModify(\'' + gridObj.getRecordIndexValue(record, 'skuProductId') + '\');">进行审核</a></div>';
			}

		}
		else {
			if(record.isRead=='1') {
				return '<div style="display: inline-block;">用户<span style="color: red;">' + record.staffName + '</span>(业务员端)发布了新的需求单,请点击<a href="javascript:transferSkuProductModify(\'' + gridObj.getRecordIndexValue(record, 'skuProductId') + '\',\''+  gridObj.getRecordIndexValue(record, 'id')+'\');">进行审核</a>' + '<span style="color: red;">未读</span></div>';
			}
			else {
				return '<div style="display: inline-block;">用户<span style="color: red;">' + record.staffName + '</span>(业务员端)发布了新的需求单,请点击<a href="javascript:transferSkuProductModify(\'' + gridObj.getRecordIndexValue(record, 'skuProductId') + '\');">进行审核</a></div>';
			}
		}
	}
	else if(record.type=='4'){
		if(record.isRead=='1') {
		     return '<div style="display: inline-block;">业务员<span style="color: red;">'+record.staffName+'</span>修改了订单，请点击,<a href="javascript:transferOrderStaffModify(\'' + gridObj.getRecordIndexValue(record, 'orderId') + '\',\''+  gridObj.getRecordIndexValue(record, 'id')+'\');">进行审核</a>'+'<span style="color: red;">未读</span></div>';
		}
		else {
			return '<div style="display: inline-block;">业务员<span style="color: red;">'+record.staffName+'</span>修改了订单，请点击,<a href="javascript:transferOrderStaffModify(\'' + gridObj.getRecordIndexValue(record, 'orderId') + '\');">进行审核</a></div>';
		}
	}
	else if(record.type=='5'){
		if(record.isRead=='1') {
			return '<div style="display: inline-block;">买家<span style="color: red;">' + record.usersName + '</span>对订单给出了差评,<a href="javascript:transferOrderCustomModify(\'' + gridObj.getRecordIndexValue(record, 'orderId') + '\',\''+  gridObj.getRecordIndexValue(record, 'id')+'\');">查看详情!</a>' + '<span style="color: red;">未读</span></div>';
		}
		else {
			return '<div style="display: inline-block;">买家<span style="color: red;">' + record.usersName + '</span>对订单给出了差评,<a href="javascript:transferOrderCustomModify(\'' + gridObj.getRecordIndexValue(record, 'orderId') + '\');">查看详情!</a></div>';
		}
	}
	else if(record.type=='6'){
		if(record.isRead=='1') {
			return '<div style="display: inline-block;">买家<span style="color: red;">' + record.buyerName + '</span>提交了资质材料,<a href="javascript:transferBuyerModify(\'' + gridObj.getRecordIndexValue(record, 'buyerId') + '\',\''+  gridObj.getRecordIndexValue(record, 'id')+'\');">前去审核</a>' + '<span  style="color: red;>未读</span></div>';
		  }
		  else {
			return '<div style="display: inline-block;">买家<span style="color: red;">' + record.buyerName + '</span>提交了资质材料,<a href="javascript:transferBuyerModify(\'' + gridObj.getRecordIndexValue(record, 'buyerId') + '\');">前去审核</a></div>';
		}
	}
	else if(record.type=='7'){
		if(record.isRead=='1') {
			return '<div style="display: inline-block;">业务员<span style="color: red;">' + record.usersName + '</span>下架了商品(卖家售完)，<a href="javascript:transferSkuProductIsOff(\'' + gridObj.getRecordIndexValue(record, 'skuProductId') + '\',\''+  gridObj.getRecordIndexValue(record, 'id')+'\');">点击查看</a>' + '<span style="color: red;>未读</span></div>';
		}
		else
		{
			return '<div style="display: inline-block;">业务员<span style="color: red;">' + record.usersName + '</span>下架了商品(卖家售完)，<a href="javascript:transferSkuProductIsOff(\'' + gridObj.getRecordIndexValue(record, 'skuProductId') + '\');">点击查看</a></div>';
		}
	}
};


// 数据表格参数查询
function doSearch() {
	var searchParames = $('#myform').serialize();
	gridObj.search(searchParames);
}

function  initTablit() {
	$.ajax({
		url:server_url+'rest/operMessage/getOperTypeNum.do?ran='+Math.random()+"&token="+token,
		type:'post',
		async:false,
		success:function(data){
			console.log(data.data);
			console.log(data.data.length);
			if(data.retCode=="0000"){
				$(".tabTit li").each(function(){
					var y = $(this);
					for(var i=0;i<data.data.length;i++){
						if(data.data[i].type==y.attr('data')) {
							var html = "<span>"+ data.data[i].num + "</span>";
							$(html).appendTo(y);
						}
					}
				});

			}
			else{
				easyDialog.open({
					container : {
						header : '提示',
						content : data.retMsg
					},
					autoClose : 2000
				});
			}
		}
	});
}

//跳转需求单审核
function transferDemand(orderId,id) {
	if(id==null) {
		var url = 'demand/modifyDemand.do?id=' + orderId;
		tl_config.loadToWorkArea(url);
	}
	else {
		//
		modifyIsRead(id);
		var url = 'demand/modifyDemand.do?id=' + orderId;
		tl_config.loadToWorkArea(url);
	}
}

//跳转订单审核
function transferOrderModify(orderId,id) {
	if(id==null) {
		var url='orderManage/modifyOrder.do?id='+orderId;
		tl_config.loadToWorkArea(url);
	}
	else {
		//
		modifyIsRead(id);
		var url='orderManage/modifyOrder.do?id='+orderId;
		tl_config.loadToWorkArea(url);
	}
}

//跳转商品发布审核
function transferSkuProductModify(skuProductId,id) {
	if(id==null) {
		var url='skuProductInfo.do?id='+skuProductId;
		tl_config.loadToWorkArea(url);
	}
	else {
		//
		modifyIsRead(id);
		var url='skuProductInfo.do?id='+skuProductId;
		tl_config.loadToWorkArea(url);
	}
}

//跳转订单修改
function transferOrderStaffModify(orderId,id) {
	if(id==null) {
		var url='orderManage/modifyOrder.do?id='+orderId;
		tl_config.loadToWorkArea(url);
	}
	else {
		//
		modifyIsRead(id);
		var url='orderManage/modifyOrder.do?id='+orderId;
		tl_config.loadToWorkArea(url);
	}
}

//跳转订单修改
function transferOrderCustomModify(orderId,id) {
	if(id==null) {
		var url='orderManage/modifyOrder.do?id='+orderId;
		tl_config.loadToWorkArea(url);
	}
	else {
		//
		modifyIsRead(id);
		var url='orderManage/modifyOrder.do?id='+orderId;
		tl_config.loadToWorkArea(url);
	}
}

//跳转客户资质审核
function transferBuyerModify(buyerId,id) {
	if(id==null) {
		var url='buyerManage/viewApproveBuyer.do?id='+buyerId;
		tl_config.loadToWorkArea(url);
	}
	else {
		//
		modifyIsRead(id);
		var url='buyerManage/viewApproveBuyer.do?id='+buyerId;
		tl_config.loadToWorkArea(url);
	}
}

//跳转商品审核页面
function transferSkuProductIsOff(skuProductId,id) {
	if(id==null) {
		var url='skuProductInfo.do?id='+skuProductId;
		tl_config.loadToWorkArea(url);
	}
	else {
		//
		modifyIsRead(id);
		var url='skuProductInfo.do?id='+skuProductId;
		tl_config.loadToWorkArea(url);
	}
}

function modifyIsRead(id) {
	$.ajax({
		url:server_url+'/rest/operMessage/modifyIsRead.do?ran='+Math.random()+"&token="+token,
		type:'get',
		data:{id:id},
		success:function(data){
			if(data.retCode=="0000"){
				//doSearch();
			}
			else{
				easyDialog.open({
					container : {
						header : '提示',
						content : data.retMsg
					},
					autoClose : 2000
				});
			}
		}
	})
}

$(".tabTit li").click(function() {
	$(this).addClass('on').siblings().removeClass('on');
	var data;
	data = $(this).attr('data');
	$("#type").val(data);
	console.log(data);
	doSearch();
});



